<?php 
/**
 * teacher 学生管理
 */
class Teacher_student extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if (!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if ($this->session->userdata('r_role_id') != 2)
            redirect('home');
    }

    /**
     * student管理的主界面
     * @return [type]
     */
    function index()
    {
        //准备数据
        $data = array();
        $data['title'] = '学生管理';

        $this->load->model('student_model');
        $this->load->model('major_model');
        // $this->load->model('class_model');
        $this->load->model('logic_class_model');

        $this->load->model('teacher_model');
        $teacher = $this->teacher_model->get_by_username($this->session->userdata('r_username'));

        $logic_classes = $this->logic_class_model->get_logic_classes_by_teacher_id($teacher['id']);
        $data['logic_classes'] = $logic_classes;
        $this->load->view('teacher_student/index', $data);
    }
    function logic_class_add()
    {
        //表单验证
        if ($this->form_validation->run('teacher_student/logic_class_add')) {
            //post方法
            //读取数据
            if (date_default_timezone_get() != "Asia/Shanghai")
                date_default_timezone_set("Asia/Shanghai");
            $logic_class_name = $this->input->post('logic_class_name');
            $teacher_id = $this->input->post('teacher_id');
            $school_id = $this->input->post('school_id');
            $big_lecture_id = $this->input->post('big_lecture_id');
            $this->load->model('logic_class_model');
            //获取当前日期
            $start_date = date("Y-m-d");
            $end_date = date("Y-m-d", strtotime("+5 month"));
            //新建一个
            $logic_class = array(
                'name' => $logic_class_name,
                'teacher_id' => $teacher_id,
                'type' => 1,
                'big_lecture_id' => $big_lecture_id,
                'school_id' => $school_id,
                'state' => 1,
                'start_date' => $start_date,
                'end_date' => $end_date
            );

            $this->logic_class_model->add($logic_class);

            $this->session->set_flashdata('message', '操作成功');
            redirect('teacher_student/index');
        }

        //get方法
        $data['title'] = '添加授课班';
        $data['school_id'] = $this->session->userdata('r_school_id');
        $data['teacher_id'] = $this->session->userdata('r_teacher_id');
        $this->load->model('big_lecture_model');
        $data['big_lectures'] = $this->big_lecture_model->get_all();
        $this->load->view('teacher_student/logic_class_add', $data);
    }
    function logic_class_delete($logic_class_id)
    {
        $this->load->model('logic_class_model');
        $this->load->model('report_model');
        //查询是否有考试信息，有责禁止删除
        $report = $this->report_model->get_by_logic_class_id($logic_class_id);
        if ($report) {
            $this->session->set_flashdata('message', '已有学生进行了考试，禁止删除班级');
            redirect('teacher_student/index');
        }
        //删除选课信息
        $this->logic_class_model->delete_student_in_logic_class($logic_class_id);
        //删除逻辑班
        if ($this->logic_class_model->delete($logic_class_id)) {
            $this->session->set_flashdata('message', '删除成功');
            redirect('teacher_student/index');
        } else {
            $this->session->set_flashdata('message', '存在相关引用，操作失败');
            redirect('teacher_student/index');
        }
    }

    /**
     * 异步jquery验证
     * @return [type] [description]
     */
    function check_logic_class_name_exist()
    {
        $logic_class_name = $this->input->post('logic_class_name');
        $school_id = $this->input->post('school_id');
        $current_logic_class_name = $this->input->post('current_logic_class_name');
        if ($current_logic_class_name) {
            if ($current_logic_class_name == $logic_class_name) {
                echo json_encode(true);
                return;
            }
        }
        $this->load->model('logic_class_model');
        if ($this->logic_class_model->logic_class_name_exist($logic_class_name, $school_id))
            echo json_encode(false);
        else
            echo json_encode(true);
    }

    /**
     * CI的表单回调函数验证
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    function logic_class_name_check($logic_class_name, $school_id)
    {
        $this->load->model('logic_class_model');
        if ($this->logic_class_model->logic_class_name_exist($logic_class_name, $school_id))
            return false;
        else
            return true;
    }

    /**
     * 查看某个逻辑班下的学生
     * @return [type] [description]
     */
    function check_logic_class($logic_class_id)
    {
        $this->load->model('logic_class_model');
        $logic_class = $this->logic_class_model->get_logic_class_by_id($logic_class_id);
        if (!$logic_class) return $this->load->view('error');
        $this->load->model('student_model');
        $students = $this->student_model->get_students_by_logic_class_id($logic_class_id);
        $this->load->model('teacher_model');
        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
        for ($i = 0; $i < count($students); $i++) {
            $students[$i]['editable'] = $this->student_model->check_student_editable($students[$i]['id'], $me['id']);
        }


        $data['logic_class'] = $logic_class;
        $data['students'] = $students;
        $data['title'] = '查看学生';
        $this->load->view('teacher_student/check_logic_class', $data);
    }

    /**
     * 查看一个学生的信息
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    function check_student($id = 0, $logic_class_id)
    {
        $this->load->model('student_model');
        $student = $this->student_model->get_student_by_id($id);
        if (!$student)
            return $this->load->view('error');
        $data['title'] = '查看学生';
        $data['before_logic_class_id'] = $logic_class_id;
        $data['student'] = $student;
        $data['relative_logic_classes'] = $this->student_model->get_relative_logic_classes($id);

        $this->load->view('teacher_student/check_student', $data);


    }

    /**
     * 删除一个学生
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function delete($logic_class_id, $student_id)
    {

        $this->load->model('student_model');
        if (!$this->student_model->get_by_id($student_id))
            $this->load->view('error');
        if ($this->student_model->delete_from_logic_class($logic_class_id, $student_id)) {
            $this->session->set_flashdata('message', '操作成功');
        } else
            $this->session->set_flashdata('message', '存在相关引用，操作失败');

        redirect('teacher_student/check_logic_class' . '/' . $logic_class_id);
    }

    /**
     * 查看一个学生的信息
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    function check_student_performance($id = 0, $logic_class_id)
    {
        $this->load->model('student_model');
        $this->load->model('major_model');
        $this->load->model('report_model');
        $this->load->model('module_model');
        $this->load->model('big_lecture_model');
        $this->load->model('logic_class_model');


        $student = $this->student_model->get_by_id($id);
        if (!$student)
            return $this->load->view('error');

        //获取学生参加的逻辑班
        $student_logic_class = $this->student_model->get_student_logic_class($student['id']);
        
        // echo "student_logic_class";
        // var_dump($student_logic_class);
        //获取所有逻辑班的信息
        $all_logic_class = array();
        foreach ($student_logic_class as $item) {
            $logic_class = $this->logic_class_model->get_by_id($item['logic_class_id']);
            array_push($all_logic_class, $logic_class);
        }

        // echo "all_logic_class";
        // var_dump($all_logic_class);

        //获取逻辑班下的考试模块
        $index = 0;
        $all_logic_class_module = array();
        foreach ($all_logic_class as $item) {
            $module_in_logic_class = $this->module_model->get_module_in_logic_class($item['big_lecture_id']);
            $big_lecture = $this->big_lecture_model->get_by_id($item['big_lecture_id']);
            //所有系统的模块和逻辑班信息叠加
            $module_add_logic_class = array();
            foreach ($module_in_logic_class as $item1) {

                //寻找每一个模块是否有可替换的定制模块
                $module_replace = $this->module_model->get_module_type_1_parent_teacher($item1['id'], $item['teacher_id']);
                // echo "module_replace";
                // var_dump($module_replace);

                //没有可替换的定制模块
                if (!$module_replace) {             
                    //在report表中查找是否已经参加了考试                  
                    $report = $this->report_model->get_by_module_student_logic_class($item1['id'], $student['id'], $item['id']);
                    // var_dump($item1['id']);
                    // var_dump($student['id']);
                    // var_dump($item['id']);
                    //var_dump($report);
                    //没有考试
                    if (!$report) {
                        $each_module_add_logic_class = array(
                            'id' => $item1['id'],
                            'name' => $item1['name'],
                            'time_limit' => $item1['time_limit'],
                            'type' => $item1['type'],
                            'parent_id' => $item1['parent_id'],
                            'teacher_id' => $item1['teacher_id'],
                            'report_id' => '0',
                            'report_datetime' => '',
                            'report_grades' => '',
                            'report_state' => 2
                        );
                    }
                    //已考试
                    else {
                        //检查当前的考试状态，根据时间判断，是否已经结束，还是可以继续进行
                        if ($report['state'] == 0) {
                            //检查考试时间
                            $now = date("Y-m-d H:i:s");
                            $this->load->helper('compare_time');
                            $time_past = compare_time_of_minute($report['datetime'], $now);
                            if ($time_past <= $item1['time_limit']) {
                                //如果还有时间继续考试
                            } else {
                                //如果已经超过时间，判卷，更新report信息
                                $this->refresh_grades($report['id']);
                                $report = $this->report_model->get_by_module_student_logic_class($item1['id'], $student['id'], $item['id']);
                            }
                        }


                        $report_date = date('Y-m-d', strtotime($report['datetime']));
                        $each_module_add_logic_class = array(
                            'id' => $item1['id'],
                            'name' => $item1['name'],
                            'time_limit' => $item1['time_limit'],
                            'type' => $item1['type'],
                            'parent_id' => $item1['parent_id'],
                            'teacher_id' => $item1['teacher_id'],
                            'report_id' => $report['id'],
                            'report_datetime' => $report['datetime'],
                            'report_grades' => $report['grades'],
                            'report_state' => $report['state']
                        );
                    }
                    array_push($module_add_logic_class, $each_module_add_logic_class);
                }

                //有替换的模块
                else {
                    foreach ($module_replace as $item2) {
                        $report_replace = $this->report_model->get_by_module_student_logic_class($item2['id'], $student['id'], $item['id']);
                        //没有考试
                        if (!$report_replace) {
                            $each_module_add_logic_class_replace = array(
                                'id' => $item2['id'],
                                'name' => $item2['name'],
                                'time_limit' => $item2['time_limit'],
                                'type' => $item2['type'],
                                'parent_id' => $item2['parent_id'],
                                'teacher_id' => $item2['teacher_id'],
                                'report_id' => '0',
                                'report_datetime' => '',
                                'report_grades' => '',
                                'report_state' => 2
                            );


                        }
                        //已考试
                        else {
                            if ($report_replace['state'] == 0) {
                                //检查考试时间
                                $now = date("Y-m-d H:i:s");
                                $this->load->helper('compare_time');
                                $time_past = compare_time_of_minute($report_replace['datetime'], $now);
                                if ($time_past <= $item1['time_limit']) {
                                    //如果还有时间继续考试
                                } else {
                                    //如果已经超过时间，判卷，更新report信息
                                    $this->refresh_grades($report_replace['id']);
                                    $report_replace = $this->report_model->get_by_module_student_logic_class($item2['id'], $student['id'], $item['id']);
                                }
                            }

                            $report_date = date('Y-m-d', strtotime($report_replace['datetime']));

                            $each_module_add_logic_class_replace = array(
                                'id' => $item2['id'],
                                'name' => $item2['name'],
                                'time_limit' => $item2['time_limit'],
                                'type' => $item2['type'],
                                'parent_id' => $item2['parent_id'],
                                'teacher_id' => $item2['teacher_id'],
                                'report_id' => $report_replace['id'],
                                'report_datetime' => $report_replace['datetime'],
                                'report_grades' => $report_replace['grades'],
                                'report_state' => $report_replace['state']
                            );
                        }
                        array_push($module_add_logic_class, $each_module_add_logic_class_replace);
                    }

                }

            }
            // echo "module_add_logic_class";
            // var_dump($module_add_logic_class);       
            $all_logic_class_module[$index] = array(
                'id' => $item['id'],
                'big_lecture_id' => $item['big_lecture_id'],
                'logic_class_id' => $item['id'],
                'logic_class_name' => $item['name'],
                'logic_class_teacher_id' => $item['teacher_id'],
                'logic_class_type' => $item['type'],
                'big_lecture_name' => $big_lecture['name'],
                'module_add_logic_class' => $module_add_logic_class
            );
            ++$index;
        }

        $major = $this->major_model->get_by_id($student['major_id']);
        $data = array();
        $data['title'] = '查看学生';
        $data['before_logic_class_id'] = $logic_class_id;
        $data['student'] = $student;
        $data['major'] = $major;
        $data['all_logic_class_module'] = $all_logic_class_module;

        $this->load->view('teacher_student/check_student_performance', $data);
    }

    //重置学生的密码
    function reset_password($logic_class_id, $id = 0)
    {
        $this->load->model('student_model');
        $student = $this->student_model->get_by_id($id);
        if (!$student)
            $this->load->view('error');
        $this->load->model('user_model');
        $user = $this->user_model->get_by_id($student['user_id']);
        //var_dump($user);
        if ($user) {
            $user['password'] = md5($student['student_id']);
            if ($this->user_model->update($user['id'], $user)) {
                $this->session->set_flashdata('message', '修改成功');
                redirect('teacher_student/check_logic_class' . '/' . $logic_class_id);
            } else {
                $this->session->set_flashdata('message', '操作失败');
                redirect('teacher_student/check_logic_class' . '/' . $logic_class_id);
            }

        } else {
            $this->session->set_flashdata('message', '不存在该用户');
            redirect('teacher_student/check_logic_class' . '/' . $logic_class_id);
        }
    }

    /**
     * 异步jquery验证
     * @return [type] [description]
     */
    function check_student_id_exist()
    {
        $student_id = $this->input->post('student_id');
        $this->load->model('student_model');
        if ($this->student_model->student_id_exist($student_id))
            echo json_encode(false);
        else
            echo json_encode(true);
    }

    /**
     * CI的表单回调函数验证
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    function student_id_check($student_id)
    {
        $this->load->model('student_model');
        if ($this->student_model->student_id_exist($student_id))
            return false;
        else
            return true;
    }

    /**
     * 添加学生
     */
    function add()
    {
		//表单验证
        if ($this->form_validation->run('teacher_student/add')) {
			//post方法
			//读取数据
            $student_name = $this->input->post('student_name');
            $student_id = $this->input->post('student_id');
            $major_id = $this->input->post('major_id');
            $logic_classes = $this->input->post('logic_classes');

			//如果用户没有选择课程（逻辑班），则报错
            if (!$logic_classes) {
                $data['error_message'] = '您必须为这个学生选择一个您的课程';
                return $this->load->view('error', $data);
            }

			//如果这个学生已经存在，核对信息
            $this->load->model('student_model');
            if ($this->student_model->student_id_exist($student_id)) {
                $student = $this->student_model->get_by_student_id($student_id);
				//添加学生选课关系
                $student_id = $student['id'];
                if ($this->student_model->add_relative_logic_classes($student_id, $logic_classes)) {
                    $this->session->set_flashdata('message', '添加成功');
                    redirect('teacher_student/index');
                }
            } else {
				//新建一个用户
                $user = array(
                    'username' => $student_id,
                    'password' => md5($student_id),
                    'role_id' => 3,
                );

                $this->load->model('user_model');
                if ($this->user_model->add($user)) {
                    $user_id = $this->db->insert_id();

					//将学生写入数据库
                    $student = array(
                        'user_id' => $user_id,
                        'name' => $student_name,
                        'student_id' => $student_id,
                        'major_id' => $major_id,
                        'type' => 1,
                        'school_id' => 7,
                    );
                    $this->load->model('student_model');
                    if ($this->student_model->add($student)) {
						//添加学生选课关系
                        $student_id = $this->db->insert_id();
                        if ($this->student_model->add_relative_logic_classes($student_id, $logic_classes)) {
                            $this->session->set_flashdata('message', '添加成功');
                            redirect('teacher_student/index');
                        }

                    }
                }
            }
            $this->session->set_flashdata('message', '操作失败');
            redirect('teacher_student/index');
        }
		//get方法
        $data['title'] = '添加学生';
        $this->load->model('major_model');
        $this->load->model('logic_class_model');
        $this->load->model('teacher_model');
        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));

        $data['majors'] = $this->major_model->get_all();
        $data['logic_classes'] = $this->logic_class_model->get_all_logic_classes_by_teacher_id($me['id']);

        $this->load->view('teacher_student/add', $data);
    }

    /**
     * 批量添加学生
     */
    function batch_add()
    {

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'text|txt';
        $config['max_size'] = '1000';
        $config['file_name'] = date('ymdHis', time());
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('student_file')) {
            $data['error_message'] = '文件不符合要求';
            $this->load->view('error', $data);
        } else {
            $file_info = $this->upload->data();
            $content = file_get_contents($file_info['full_path']);
            $content = $this->remove_utf8_bom($content);
			// $content = mb_convert_encoding($content, "UTF-8");
            $first_line = strtok($content, "\n");
            $second_line = strtok("\n");
            $third_line = strtok("\n");
            $item = strtok("\n");
            $items = array();
            while ($item) {
                array_push($items, $item);
                $item = strtok("\n");
            }

            $teacher_name = strtok($first_line, ",");
            $teacher_name = substr($teacher_name, 1);
            $teacher_name = trim($teacher_name);
            $teacher_number = strtok("\n");
            $teacher_number = trim($teacher_number);
            $this->load->model('teacher_model');
            $teacher_exist = $this->teacher_model->teacher_number_exist($teacher_number);
            $teacher_state = false;
            if (!$teacher_exist)
                $teacher_state = true;
            else {
                $teacher = $this->teacher_model->get_by_teacher_number($teacher_number);
                if ($teacher_name == $teacher['name'])
                    $teacher_state = true;
            }

            $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
            if (!$teacher_exist)
                $teacher_state = false;
            else {
                $teacher = $this->teacher_model->get_by_teacher_number($teacher_number);

                if ($teacher_name == $teacher['name'] && $teacher['teacher_number'] == $me['teacher_number'])
                    $teacher_state = true;
            }
            $big_lecture_name = strtok($second_line, ",");
            $big_lecture_name = substr($big_lecture_name, 1);
            $big_lecture_name = trim($big_lecture_name);
            $logic_class_name = strtok("\n");
            $logic_class_name = trim($logic_class_name);
            $this->load->model('logic_class_model');
            $this->load->model('big_lecture_model');

            $logic_class_exist = $this->logic_class_model->logic_class_number_exist($logic_class_name);

            $logic_class_state = false;
			//检查课程名称是否存在
            $this->load->model('big_lecture_model');
            $lecture_exist = $this->big_lecture_model->check_lecture_exist($logic_class_name);
            if (!$lecture_exist)
                $logic_class_state = false;
            else {
                if (!$logic_class_exist)
                    $logic_class_state = true;
                else {

                    $logic_class = $this->logic_class_model->get_by_logic_class_number($logic_class_number);
                    if ($logic_class_name == $logic_class['big_lecture_name'] && $logic_class['teacher_id'] == $me['id'])
                        $logic_class_state = true;
                }
            }

            $this->load->model('student_model');
            $students = array();
            foreach ($items as $item) {
                $student['student_id'] = strtok($item, ',');
                $student['student_id'] = trim($student['student_id']);
                $student['student_name'] = strtok("\n");
                $student['student_name'] = trim($student['student_name']);
                $student['student_exist'] = $this->student_model->student_id_exist($student['student_id']);

                $student['student_state'] = false;
                if (!$student['student_exist'])
                    $student['student_state'] = true;
                else {
                    $stu = $this->student_model->get_by_student_id($student['student_id']);
                    if ($student['student_name'] == $stu['name'])
                        $student['student_state'] = true;
                }
                if (!($student['student_id'] && $student['student_name'])) {
                    $data['error_message'] = '文件信息不完整，或格式错误1';
                    $this->load->view('error', $data);
                }
                array_push($students, $student);
            }
        
			// if(!($teacher_name&&$teacher_number&&$logic_class_name&&$logic_class_number&&$class_name&&$major_name))
			// {
			// 	$data['error_message']='文件信息不完整，或格式错误2';
			// 	return $this->load->view('error',$data);
			// }	

			// $data['class_name'] = $class_name;
			// $data['class_exist'] = $class_exist;
			// $data['major_name']=$major_name;
			// $data['major_exist']=$major_exist;



            // $logic_class_name = strtok($second_line, ",");
            // $logic_class_name = trim($logic_class_name);
            // $logic_class_name = substr($logic_class_name, 1);
            // $logic_class_name = trim($logic_class_name);
            // $logic_class_number = strtok("\n");
            // $logic_class_number = trim($logic_class_number);

			// // $class_name = strtok($second_line, ",");
			// // $class_name = substr($class_name, 1);
			// // $class_name = trim($class_name);
			// // $this->load->model('class_model');
			// // $class_exist = $this->class_model->check_class_name_exist($class_name);

            $this->load->model('logic_class_model');
            $logic_class_exist = $this->logic_class_model->logic_class_number_exist($logic_class_name);

            $logic_class_state = false;
			//检查课程名称是否存在
            $this->load->model('big_lecture_model');
            $lecture_exist = $this->big_lecture_model->check_lecture_exist($logic_class_name);
            if (!$lecture_exist)
                $logic_class_state = false;
            else {
                if (!$logic_class_exist)
                    $logic_class_state = true;
                else {

                    $logic_class = $this->logic_class_model->get_by_logic_class_number($logic_class_name);
                    if ($logic_class_name == $logic_class['big_lecture_name'] && $logic_class['teacher_id'] == $me['id'])
                        $logic_class_state = true;
                }
            }
            // $major_name = strtok("\n");
            // $major_name = trim($major_name);
            $this->load->model('major_model');
            $major_exist = $this->major_model->check_major_name_exist_in_a_school($major_name);
            $major_exist = 1;
            // var_dump($major_name);
            // var_dump($major_exist);

            // $this->load->model('student_model');
            // $students = array();
            // foreach ($items as $item) {
            //     $student['student_id'] = strtok($item, ',');
            //     $student['student_id'] = trim($student['student_id']);
            //     $student['student_name'] = strtok("\n");
            //     $student['student_name'] = trim($student['student_name']);
            //     $student['student_exist'] = $this->student_model->student_id_exist($student['student_id']);

            //     $student['student_state'] = false;
            //     if (!$student['student_exist'])
            //         $student['student_state'] = true;
            //     else {
            //         $stu = $this->student_model->get_by_student_id($student['student_id']);
            //         if ($student['student_name'] == $stu['name'])
            //             $student['student_state'] = true;
            //     }
            //     if (!($student['student_id'] && $student['student_name'])) {
            //         $data['error_message'] = '文件信息不完整，或格式错误1';
            //         $this->load->view('error', $data);
            //     }
            //     array_push($students, $student);
            // }

			// // if(!($teacher_name&&$teacher_number&&$logic_class_name&&$logic_class_number&&$class_name&&$major_name))
			// // {
			// // 	$data['error_message']='文件信息不完整，或格式错误2';
			// // 	return $this->load->view('error',$data);
			// // }	
            // $data['teacher_name'] = $teacher_name;
            // $data['teacher_number'] = $teacher_number;
            // $data['teacher_exist'] = $teacher_exist;
            // $data['teacher_state'] = $teacher_state;

			// // $data['class_name'] = $class_name;
            // // $data['class_exist'] = $class_exist;

            $major = $this->teacher_model->findMajorById($teacher['id']);
            $major_name = $major['name'];
            $major_id = $major['id'];


            $data['major_name'] = $major_name;
            $data['major_exist'] = $major_exist;
            $data['students'] = $students;
            $data['major_id'] = $major_id;
            $data['teacher_name'] = $teacher_name;
            $data['teacher_number'] = $teacher_number;
            $data['teacher_exist'] = $teacher_exist;
            $data['teacher_state'] = $teacher_state;
            $data['logic_class_name'] = $logic_class_name;
            $data['logic_class_exist'] = $logic_class_exist;
            $data['logic_class_state'] = $logic_class_state;
            $data['big_lecture_name'] = $big_lecture_name;
            $data['title'] = '批量添加预览';
            echo ($major_id);
			//var_dump($data);
            $this->load->view('teacher_student/batch_add_preview', $data);
			



		// $data = array('upload_data' => $this->upload->data());

		// $this->load->view('upload_success', $data);
        }
    }

    /**
     * 检测并移除字符串中的bom
     * @param  [type] $text [description]
     * @return [type]       [description]
     */
    function remove_utf8_bom($text)
    {
        $bom = pack('H*', 'EFBBBF');
        $text = preg_replace("/^$bom/", '', $text);
        return $text;
    }


    /**
     * 批量添加学生确认
     * @return [type] [description]
     */
    function batch_add_confirm()
    {
			//加载需要的模型
        $this->load->model('teacher_model');
        $this->load->model('logic_class_model');
        $this->load->model('major_model');
        $this->load->model('user_model');
        $this->load->model('student_model');
	
			//读取数据
        $teacher_name = $this->input->post('teacher_name');
        $teacher_number = $this->input->post('teacher_number');
        $logic_class_name = $this->input->post('logic_class_name');
        $major_name = $this->input->post('major_name');
        $student_count = $this->input->post('student_count');
        $logic_class_state = $this->input->post('logic_class_state');
        $major =  $this->major_model->get_by_major_name($major_name);
        $major_id = $major['id'];
        echo($major_id);
			
			//验证信息是否完整
			// if(!($teacher_name&&$teacher_number&&$$class_name))
			// {
			// 	$data['error_message']='文件信息不完整，或格式错误3';
			// 	return $this->load->view('error',$data);
			// }	



        $teacher_exist = $this->teacher_model->teacher_number_exist($teacher_number);
        // $class_exist = $this->class_model->check_class_name_exist($class_name);
			// $logic_class_exist=$this->logic_class_model->logic_class_number_exist($logic_class_number);
			// $major_exist=$this->major_model->check_major_name_exist($major_name);
	
			//如果专业不存在错误
			// if(!$major_exist)
			// {
			// 	$data['error_message']='专业不存在';
			// 	return $this->load->view('error',$data);
			// }
			// $major=$this->major_model->get_by_major_name($major_name);
	
			//如果班级不存在，错误
			//如果老师不存在，错误
        if (!$teacher_exist) {
            $data['error_message'] = '这个老师不存在';
            return $this->load->view('error', $data);
        }
        $teacher = $this->teacher_model->get_by_teacher_number($teacher_number);
        $this->load->model('teacher_model');
        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
			//如果不是自己，错误
        if ($teacher['teacher_number'] != $me['teacher_number']) {
            $data['error_message'] = '必须添加自己的学生';
            return $this->load->view('error', $data);
        }
	
		// 	//逻辑班如果不存在新建一个
        // if (!$logic_class_exist) {
        //     //通过名称得到课程id
        //     $big_lecture_name = $logic_class_name;
        //     $this->load->model('lecture_model');
        //     $big_lecture_id = $this->big_lecture_model->get_lecture_id($big_lecture_name);
        //     $logic_class = array(
        //         'name' => $logic_class_name,
        //         'teacher_id' => $teacher['id'],
        //         'type' => 1,
        //         'big_lecture_id' => $big_lecture_id
        //     );
        //     $this->logic_class_model->add($logic_class);
        // }
        // $logic_class = $this->logic_class_model->get_by_logic_class_number($logic_class_name);


        for ($i = 1; $i <= $student_count; $i++) {
            $student_name = $this->input->post('student_name_' . $i);
            $student_id = $this->input->post('student_id_' . $i);
            $student_exist = $this->student_model->student_id_exist($student_id);
				//如果存在这个用户，合并
				// if($student_exist)
				// {
				// 	$student=$this->student_model->get_by_student_id($student_id);
				// 	$this->student_model->delete_cascade($student['id']);
				// }
				//如果不存在新建一个
            if (!$student_exist) {
					//新建一个用户
                $user = array(
                    'username' => $student_id,
                    'password' => md5($student_id),
                    'role_id' => 3,
                );

                if ($this->user_model->add($user)) {
                    $user_id = $this->db->insert_id();

                    
	
						//将学生写入数据库
                    $student = array(
                        'user_id' => $user_id,
                        'name' => $student_name,
                        'student_id' => $student_id,
                        'major_id' => $major_id,
                        'type' => 1,
                        'school_id' => 7
                    );
                    $this->student_model->add($student);
                }
            }


            $student = $this->student_model->get_by_student_id($student_id);
	
				//录入选课信息
				// $this->student_model->add_logic_class($student['id'],$logic_class['id']);

        }

        $this->session->set_flashdata('message', '操作成功');
        redirect('teacher_student/index');

    }

    /**
     *查看逻辑班的考试模块
     */
    function check_module_in_logic_class($logic_class_id, $class_id = 0)
    {
		//获取这个逻辑班的课程中的所有考试模块
        $this->load->model('logic_class_model');
        $this->load->model('module_model');
        $this->load->model('big_lecture_model');
        $this->load->model('teacher_model');

        $teacher = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
        $logic_class = $this->logic_class_model->get_by_id($logic_class_id);
		//var_dump($logic_class);
        $module_in_logic_class = $this->module_model->get_module_in_logic_class($logic_class['big_lecture_id']);
		//var_dump($module_in_logic_class);
        $lecture_info = $this->big_lecture_model->get_lecture_by_id($logic_class['big_lecture_id']);
        $module_add_logic_class = array();
        foreach ($module_in_logic_class as $module_in_logic_class_item) {
			//寻找每一个模块是否有可替换的定制模块
            $module_replace = $this->module_model->get_module_type_1_parent_teacher($module_in_logic_class_item['id'], $teacher['id']);
            if ($module_replace) {
                foreach ($module_replace as $module_replace_item) {
                    $each_module_add_logic_class_replace = array(
                        'module_id' => $module_replace_item['id'],
                        'name' => $module_replace_item['name'],
                        'time_limit' => $module_replace_item['time_limit'],
                        'type' => $module_replace_item['type'],
                        'parent_id' => $module_replace_item['parent_id'],
                        'teacher_id' => $module_replace_item['teacher_id'],
                        'big_lecture_id' => $module_replace_item['big_lecture_id'],
                        'logic_class_id' => $logic_class['id'],
                        'logic_class_teacher_id' => $logic_class['teacher_id'],
                        'logic_class_type' => $logic_class['type'],
                        'big_lecture_id' => $logic_class['big_lecture_id'],
                        'big_lecture_name' => $lecture_info['name'],
                        // 'big_lecture_name' => $lecture_info['big_lecture_name']
                    );
                    array_push($module_add_logic_class, $each_module_add_logic_class_replace);
                }

            } else {
                $each_module_add_logic_class = array(
                    'module_id' => $module_in_logic_class_item['id'],
                    'name' => $module_in_logic_class_item['name'],
                    'time_limit' => $module_in_logic_class_item['time_limit'],
                    'type' => $module_in_logic_class_item['type'],
                    'parent_id' => $module_in_logic_class_item['parent_id'],
                    'teacher_id' => $module_in_logic_class_item['teacher_id'],
                    'big_lecture_id' => $module_in_logic_class_item['big_lecture_id'],
                    'logic_class_id' => $logic_class['id'],
                    'logic_class_teacher_id' => $logic_class['teacher_id'],
                    'logic_class_type' => $logic_class['type'],
                    'big_lecture_id' => $logic_class['big_lecture_id'],
                    'big_lecture_name' => $lecture_info['name'],
                    // 'big_lecture_name' => $lecture_info['big_lecture_name']
                );
                array_push($module_add_logic_class, $each_module_add_logic_class);
            }
        }
		//var_dump($module_add_logic_class);
        $data = array();
        $data['title'] = '考试知识点错误分析';
        $data['teacher'] = $teacher;
        $data['class_id'] = $class_id;
        $data['module_add_logic_class'] = $module_add_logic_class;
        if ($class_id) {
            $this->load->view('teacher_student/check_module_in_logic_class_student', $data);
        } else {
            $this->load->view('teacher_student/check_module_in_logic_class', $data);
        }

    }


}
 